package org.cboard.util;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.facebook.presto.jdbc.internal.guava.collect.Lists;
import com.google.common.collect.Maps;

public class TreeList {
	private int id;
	private int pId;
	private String parent;
	private String text;
	
	private transient boolean root=true;
	
	private transient List<TreeList> children=Lists.newArrayList();
	
	public static List<TreeList> getList(List<TreeList> list){
		Map<Integer, TreeList> map = Maps.newHashMap();
		for(TreeList tt:list){
			map.put(tt.getId(), tt);
		}
        list.forEach(tt->{if(tt.getpId()>0){
            map.get(tt.getpId()).getChildren().add(tt);
            tt.setRoot(false);
        }});

		for(int i=list.size()-1;i>=0;i--){
			TreeList tt = list.get(i);
			if(!tt.isRoot()){
				list.remove(i);
			}
		}
		return list;
	}

    public String getParent() {
        return parent;
    }

    public void setParent(String parent) {
        this.parent = parent;
    }

    public static String toTreeJson(List<TreeList> list){
		if(list.size()<=0){
			return null;
		}
		return JSON.toJSONString(list);
	}
	
	
	public boolean isRoot() {
		return root;
	}

	public void setRoot(boolean root) {
		this.root = root;
	}

	public List<TreeList> getChildren() {
		return children;
	}

	public void setChildren(List<TreeList> children) {
		this.children = children;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

    public int getpId() {
        return pId;
    }

    public void setpId(int pId) {
        this.pId = pId;
    }

    public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	 
	
	
			

}
